package com.hr.剑指offer.work1;


/**
 * @author 周恒哲
 * @date 2020/06/12
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 * 示例 1：
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 **/
public class 面试题05_替换空格 {
    public static String replaceSpace(String s) {
        return s.replaceAll(" ", "%20");//将空格替换成%20
    }
    public static String replaceSpace2(String s) {
        int len=s.length();
        char[] chars = new char[3*len];
        int size = 0;
        for (int i = 0; i < s.length(); i++) {
            char c=s.charAt(i);
            if (c==' '){
                chars[size++]='%';
                chars[size++]='2';
                chars[size++]='0';
            }else{
                chars[size++]=c;
            }
        }
        String newStr = new String(chars,0,size);
        return newStr;
    }

    public static void main(String[] args) {
        System.out.println(面试题05_替换空格.replaceSpace("We are happy."));
    }
}
